package cn.tedu.aop;

import cn.tedu.vo.SysResult;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class SysResultException {
    @ExceptionHandler(RuntimeException.class)
    public Object exception(Exception e, HttpServletRequest request) {
        String callback = request.getParameter("callback");
        if (!StringUtils.isEmpty(callback)) {
            return new JSONPObject(callback, SysResult.fail());
        } else {
            e.printStackTrace();
            return SysResult.fail();
        }
    }
}
